package org.karesz.services;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.karesz.dao.UserDao;
import org.karesz.model.Rights;
import org.karesz.model.User;


@ManagedBean
@RequestScoped
public class UserServices {
	
	
	@EJB
	UserDao dao;
	
	FacesContext context;
	
	private User user;
	
	@PostConstruct
	private void init(){
		this.context = FacesContext.getCurrentInstance();
	}
	
    public Rights[] getRightsList() {
        return Rights.values();
    }
    
	public User getUser() {
		if(this.user == null) this.user = new User();
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public String action(){
		user.setCreated(new Date());
		dao.save(user);
		context.addMessage(null, new FacesMessage("Successful",  "Saved!!!: ") );
		this.user.setName("");
		this.user.setAge(0);
		return "#";
	}
	
/*
    public void saveMessage() {
        FacesContext context = FacesContext.getCurrentInstance();
         
        context.addMessage(null, new FacesMessage("Successful",  "Your message: " + message) );
        context.addMessage(null, new FacesMessage("Second Message", "Additional Message Detail"));
    }
*/
	
}
